<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Exercise 4.3: Solve a simple QP with inequality constraints</title>
<link rel="canonical" href="http://cvxr.com/cvx/examples/cvxbook/Ch04_cvx_opt_probs/html/ex_4_3.html">
<link rel="stylesheet" href="../../../examples.css" type="text/css">
</head>
<body>
<div id="header">
<h1>Exercise 4.3: Solve a simple QP with inequality constraints</h1>
Jump to:&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#source">Source code</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#output">Text output</a>
&nbsp;&nbsp;&nbsp;&nbsp;
Plots
&nbsp;&nbsp;&nbsp;&nbsp;<a href="../../../index.html">Library index</a>
</div>
<div id="content">
<a id="source"></a>
<pre class="codeinput">
<span class="comment">% From Boyd &amp; Vandenberghe, "Convex Optimization"</span>
<span class="comment">% Jo&euml;lle Skaf - 09/26/05</span>
<span class="comment">%</span>
<span class="comment">% Solves the following QP with inequality constraints:</span>
<span class="comment">%           minimize    1/2x'*P*x + q'*x + r</span>
<span class="comment">%               s.t.    -1 &lt;= x_i &lt;= 1      for i = 1,2,3</span>
<span class="comment">% Also shows that the given x_star is indeed optimal</span>

<span class="comment">% Generate data</span>
P = [13 12 -2; 12 17 6; -2 6 12];
q = [-22; -14.5; 13];
r = 1;
n = 3;
x_star = [1;1/2;-1];

<span class="comment">% Construct and solve the model</span>
fprintf(1,<span class="string">'Computing the optimal solution ...'</span>);
cvx_begin
    variable <span class="string">x(n)</span>
    minimize ( (1/2)*quad_form(x,P) + q'*x + r)
    x &gt;= -1;
    x &lt;=  1;
cvx_end
fprintf(1,<span class="string">'Done! \n'</span>);

<span class="comment">% Display results</span>
disp(<span class="string">'------------------------------------------------------------------------'</span>);
disp(<span class="string">'The computed optimal solution is: '</span>);
disp(x);
disp(<span class="string">'The given optimal solution is: '</span>);
disp(x_star);
</pre>
<a id="output"></a>
<pre class="codeoutput">
Computing the optimal solution ... 
Calling sedumi: 11 variables, 7 equality constraints
------------------------------------------------------------
SeDuMi 1.21 by AdvOL, 2005-2008 and Jos F. Sturm, 1998-2003.
Alg = 2: xz-corrector, Adaptive Step-Differentiation, theta = 0.250, beta = 0.500
eqs m = 7, order n = 9, dim = 12, blocks = 2
nnz(A) = 17 + 0, nnz(ADA) = 33, nnz(L) = 20
 it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
  0 :            2.75E+00 0.000
  1 :  -3.71E+01 7.86E-01 0.000 0.2860 0.9000 0.9000   2.64  1  1  8.3E-01
  2 :  -5.38E+01 1.87E-01 0.000 0.2381 0.9000 0.9000   1.14  1  1  1.9E-01
  3 :  -5.78E+01 4.88E-02 0.000 0.2607 0.9000 0.9000   0.95  1  1  5.2E-02
  4 :  -5.88E+01 1.20E-02 0.000 0.2464 0.9000 0.9000   0.95  1  1  1.3E-02
  5 :  -5.90E+01 2.77E-03 0.000 0.2306 0.9000 0.9000   0.97  1  1  3.0E-03
  6 :  -5.91E+01 4.56E-04 0.000 0.1647 0.9015 0.9000   0.99  1  1  5.7E-04
  7 :  -5.91E+01 4.96E-06 0.000 0.0109 0.9901 0.9900   1.00  1  1  1.3E-05
  8 :  -5.91E+01 2.30E-10 0.000 0.0000 0.9999 0.9969   1.00  1  1  2.8E-08
  9 :  -5.91E+01 1.37E-11 0.000 0.0596 0.9450 0.9475   1.00  2  2  1.6E-09

iter seconds digits       c*x               b*y
  9      0.1   Inf -5.9124999997e+01 -5.9124999919e+01
|Ax-b| =   5.9e-10, [Ay-c]_+ =   1.8E-08, |x|=  4.2e+00, |y|=  3.0e+01

Detailed timing (sec)
   Pre          IPM          Post
1.000E-02    5.000E-02    1.000E-02    
Max-norms: ||b||=2, ||c|| = 4.268278e+01,
Cholesky |add|=0, |skip| = 0, ||L.L|| = 1.631.
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): -21.625
Done! 
------------------------------------------------------------------------
The computed optimal solution is: 
    1.0000
    0.5000
   -1.0000

The given optimal solution is: 
    1.0000
    0.5000
   -1.0000

</pre>
</div>
</body>
</html>